package mm.chenchen.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import mm.chenchen.common.constant.SeqConstant;
import mm.chenchen.constant.ResultCode;
import mm.chenchen.constant.ResultMsg;

import mm.chenchen.constant.StatusCode;
import mm.chenchen.db.annotation.DataSource;
import mm.chenchen.db.model.DataSourceCreate;
import mm.chenchen.db.service.DynamicDataSourceService;
import mm.chenchen.modules.sys.dao.FastStartSeqDao;
import mm.chenchen.modules.sys.service.FastStartSeqService;
import mm.chenchen.utils.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import mm.chenchen.utils.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mm.chenchen.modules.sys.entity.FastStartDataSource;
import mm.chenchen.modules.sys.dao.FastStartDataSourceDao;
import mm.chenchen.modules.sys.service.FastStartDataSourceService;
import mm.chenchen.utils.PageUtils;
import mm.chenchen.utils.PageModel;

import java.util.List;
import java.util.Map;

import mm.chenchen.modules.sys.dao.model.request.FastStartDataSourceQuery;
import org.springframework.util.ObjectUtils;

import javax.annotation.PostConstruct;


@Service("fastStartDataSourceService")
////@DataSource("base")
@Slf4j
public class FastStartDataSourceServiceImpl extends ServiceImpl<FastStartDataSourceDao, FastStartDataSource> implements FastStartDataSourceService {
    @Autowired
    private DynamicDataSourceService dynamicDataSourceService;
    @Autowired
    private FastStartSeqService seqService;

    public void initDataSource() {
        log.info("从数据库初始化数据源!");
        QueryWrapper<FastStartDataSource> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(FastStartDataSource::getValid, StatusCode.VALID);
        List<FastStartDataSource> list = list(queryWrapper);
        list.forEach(ele -> {
            DataSourceCreate create = new DataSourceCreate();
            create.setDataSourceName(ele.getDataSourceName());
            create.setUrl(ele.getUrl());
            create.setUsername(ele.getUser());
            create.setPassword(ele.getPassword());
            create.setDriverClassName(ele.getDriverClass());
            create.setDbType(ele.getDbType());
            dynamicDataSourceService.addDataSource(create);
        });
    }

    public R findPage(Page<Map> page, FastStartDataSourceQuery param) {
        IPage<FastStartDataSource> pageData = baseMapper.findPage(page, param);
        if (pageData != null) {
            return R.ok().setData(new PageModel(pageData.getRecords(), pageData.getTotal(), pageData.getSize(), pageData.getCurrent()));
        } else {
            return R.error(ResultMsg.DATA_NOT_EXIT);
        }
    }

    public R getById(Integer id) {
        return R.ok().setData(super.getById(id));
    }


    public R saveInfo(FastStartDataSource param) {
        param.setId(seqService.nextVal(SeqConstant.DATA_SOURCE_SEQ_ID_NAME));
        boolean bb = super.save(param);
        if (!StatusCode.VALID.equals(param.getValid())) {
            return R.ok().setData(bb);
        }
        DataSourceCreate create = new DataSourceCreate();
        create.setDataSourceName(param.getDataSourceName());
        create.setUrl(param.getUrl());
        create.setUsername(param.getUser());
        create.setPassword(param.getPassword());
        create.setDriverClassName(param.getDriverClass());
        create.setDbType(param.getDbType());
        if (dynamicDataSourceService.addDataSource(create).getCode() == ResultCode.SUCCESS) {
            return R.ok().setData(bb);
        } else {
            return R.error(ResultMsg.FAILED_TO_CONNECT_DATABASE);
        }
    }


    public R update(FastStartDataSource param) {
        FastStartDataSource data = super.getById(param.getId());
        if (ObjectUtils.isEmpty(data)) {
            return R.error(ResultMsg.DATA_NOT_EXIT);
        }
        if (!StatusCode.VALID.equals(param.getValid())) {
            DataSourceCreate create = new DataSourceCreate();
            create.setDataSourceName(param.getDataSourceName());
            create.setUrl(param.getUrl());
            create.setUsername(param.getUser());
            create.setPassword(param.getPassword());
            create.setDriverClassName(param.getDriverClass());
            create.setDbType(param.getDbType());
            dynamicDataSourceService.addDataSource(create);
        }
        return R.ok().setData(super.updateById(param));
    }


    public R deleteById(Integer id) {
        return R.ok().setData(super.removeById(id));
    }


}